﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Ionic.Zip;

namespace SPThumbnailExtender.Interne
{
    internal class ThumbnailZip : ThumbnailBase
    {
        public ThumbnailZip(Uri uri)
            : base(uri)
        { }

        public ThumbnailZip(Uri uri, int Width, int Height)
            : base(uri, Width, Height)
        { }
        public override System.Drawing.Bitmap GenerateThumbnail()
        {
            return GenerateThumbnail(0);
        }

        public override System.Drawing.Bitmap GenerateThumbnail(int Page)
        {
            
            using (ZipFile zip = ZipFile.Read(this.GetFileStream()))
            {
                
                Bitmap bmpReturn = new Bitmap(640, 480);

                Graphics grfx = Graphics.FromImage(bmpReturn);
                grfx.Clear(Color.White);
                Font ft = new Font("Verdana", 10);
                float Top = 10;
                SolidBrush brush = new SolidBrush(Color.Black);
                Pen pen = new Pen(brush);
                pen.Width = 2;
                grfx.DrawRectangle(pen, 5, 5, 630, 470);
                foreach (ZipEntry ze in zip)
                {
                    string strUriPart = ze.FileName;
                    int level = 0;
                    if (strUriPart.LastIndexOf('/') == strUriPart.Length - 1)
                    {
                        string strUriFolder = strUriPart.Substring(0, strUriPart.Length - 1);
                        level = strUriFolder.Split('/').Length;
                        int iStartIndex = strUriFolder.LastIndexOf('/') + 1;
                        int iLength = strUriPart.Length - iStartIndex;
                        strUriPart = strUriPart.Substring(iStartIndex, iLength);
                    }
                    else
                    {
                        level = strUriPart.Split('/').Length;
                        int iStartIndex = strUriPart.LastIndexOf('/') + 1;
                        int iLength = strUriPart.Length - iStartIndex;
                        strUriPart = strUriPart.Substring(iStartIndex, iLength);
                    }
                    SizeF sizeText = grfx.MeasureString(strUriPart, ft);

                    grfx.DrawString(strUriPart, ft, brush, new PointF(10.0f + 10 * level, Top));
                    Top += sizeText.Height;
                    if (Top > bmpReturn.Height)
                        break;
                }
                brush.Dispose();
                grfx.Flush();
                grfx.Dispose();
                Helper.DeleteThumbnailsForDocument(this.FileUri);
                Helper.InsertThumbnail(this.FileUri, 0, bmpReturn);
                return bmpReturn;
            }
        }

        public override void GenerateThumbnails()
        {
            Helper.DeleteThumbnailsForDocument(this.FileUri);

            GenerateThumbnail();
            

        }

        public override int GetCount()
        {
            return 1;
        }
    }
}
